package cn.youqu.happy.module.youqu.controller.app.userBlack;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userBlack.vo.UserBlackPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userBlack.vo.AppUserBlackJoinReqVO;
import cn.youqu.happy.module.youqu.controller.app.userBlack.vo.AppUserBlackPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userBlack.vo.AppUserBlackRespVO;
import cn.youqu.happy.module.youqu.controller.app.userBlack.vo.AppUserBlackUpdateReqVO;
import cn.youqu.happy.module.youqu.convert.userBlack.UserBlackConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userBlack.UserBlackDO;
import cn.youqu.happy.module.youqu.service.userBlack.UserBlackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户黑名单")
@RestController
@RequestMapping("/youqu/user-black")
@Validated
public class AppUserBlackController {

    @Resource
    private UserBlackService userBlackService;

    @PostMapping("/join")
    @ApiOperation("加入用户黑名单")
    @PreAuthenticated
    public CommonResult<Integer> joinUserBlack(@Valid @RequestBody AppUserBlackJoinReqVO createReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userBlackService.joinUserBlack(createReqVO, loginUserId));
    }
    @PutMapping("/update")
    @ApiOperation("更新用户黑名单")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserBlack(@Valid @RequestBody AppUserBlackUpdateReqVO updateReqVO) {
        Long loginUserId = getLoginUserId();

        userBlackService.updateUserBlack(updateReqVO,loginUserId);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户黑名单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserBlack(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        userBlackService.deleteUserBlack(id, loginUserId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户黑名单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserBlackRespVO> getUserBlack(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        UserBlackDO userBlack = userBlackService.getUserBlack(id, loginUserId);
        return success(UserBlackConvert.INSTANCE.convert0(userBlack));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户黑名单列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppUserBlackRespVO>> getUserBlackList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<UserBlackDO> list = userBlackService.getUserBlackList(ids, loginUserId);
        return success(UserBlackConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户黑名单分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserBlackRespVO>> getUserBlackPage(@Valid AppUserBlackPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserBlackPageReqVO convert = UserBlackConvert.INSTANCE.convert(pageVO);
        PageResult<UserBlackDO> pageResult = userBlackService.getUserBlackPage(convert, loginUserId);
        return success(UserBlackConvert.INSTANCE.convertPage0(pageResult));
    }


}
